home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / CONSTRAI / OP0_IMPL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  11.7 KB  |  311 lines

  1.  
  2. package sub_arctic.constraints;
  3.  
  4. import sub_arctic.lib.interactor;
  5. import sub_arctic.lib.manager;
  6. import sub_arctic.lib.sub_arctic_error;
  7.  
  8. import java.util.Vector;
  9.  
  10. /** 
  11.  * Constraint implementation class to provide encoding for standard 0 operand 
  12.  * lightweight constraints (constraint with only implicit operands).  This 
  13.  * object takes a signed 16 bit value to provide a constant to the function.<p>
  14.  *
  15.  * 0 operand constraints are encoded as:
  16.  * <pre>
  17.  *             16          6     5     5
  18.  *     KKKKKKKKKKKKKKKK XXXXXX 00000 00001   64 0 operand operations
  19.  *                                             [K is 16 bit signed]
  20.  * </pre>
  21.  * where KKK represents a 16 bit signed constant, and XXX represents bits 
  22.  * used to encode an op code.<p>
  23.  *
  24.  * @author Scott Hudson
  25.  */
  26. public class op0_impl implements std_encoding_consts {
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /** 
  31.    * Create a standard constraint given an op code and constant.<p>
  32.    * 
  33.    * @param int   op_code op code value for this operation.
  34.    * @param short K       value for 16 bit signed constant
  35.    */
  36.   public static std_constraint create(int op_code, short K) 
  37. {
  38.       return new std_constraint(encode(op_code,K), 0);
  39.     }
  40.  
  41.    //had:
  42.    //* @exception bad_constraint if the given op_code is out of range
  43.  
  44.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  45.  
  46.   /** 
  47.    * Do an encoding given an op code and constant.<p>
  48.    * 
  49.    * @param int   op_code op code value for this operation.
  50.    * @param short K       value for 16 bit signed constant
  51.    */
  52.   public static int encode(int op_code, short K) 
  53. {
  54.       if (op_code < OP0_MIN || op_code > OP0_MAX)
  55.     throw new sub_arctic_error(
  56.      "Unrecognized op code (" + op_code + ") used for 0 op constraint");
  57.  
  58.       return (((int)K)<<16) | ((op_code<<OP0_SHIFT) & OP0_MASK)  | OP0_LOBITS;
  59.     }
  60.  
  61.    //had:
  62.    //* @exception bad_constraint if the given op_code is out of range
  63.  
  64.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  65.  
  66.   /** 
  67.    * Decode op_code from given encoding.  Note: this assumes it is already
  68.    * known that the given encoding is for a 0 op constraint.  If that is
  69.    * not the case results will wrong.<p>
  70.    *
  71.    * @param int enc the encoding for a 0-op constraint.
  72.    * @return int the op code value for the constraint.
  73.    */
  74.   public static int op_code(int enc) { return (enc & OP0_MASK) >> OP0_SHIFT; }
  75.  
  76.   /** 
  77.    * Decode constant from given encoding. Note: this assumes it has already 
  78.    * known that the given encoding is for a 0 op constraint.  If that is 
  79.    * not the case results will be wrong.<p>
  80.    *
  81.    * @param int enc the encoding for a 0-op constraint.
  82.    * @return int the constant value for the constraint.
  83.    */
  84.   public static short const_val(int enc) { return (short)((enc>>16) & 0xffff); }
  85.  
  86.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  87.  
  88.   /**
  89.    * Evaluate an encoded constraint function given its constant value.  We 
  90.    * assume here that it is has already been determined that the constraint 
  91.    * is a 0-op constraint.  Note: we don't support OP_external or OP_NONE
  92.    * here because this is called from the outside by external functions and 
  93.    * this is an error and/or we would get into infinite recursion.  OP_external 
  94.    * and OP_NONE are handled as a special case in eval().<p>
  95.    *
  96.    * @param int        enc         the encoding from the constraint
  97.    * @param interactor constr_obj  the object being constrained
  98.    * @param int        cnst_val    value of constant parameter
  99.    * @param int        orient      orientation of the constraint (should be
  100.    *                               HORIZONTAL or VERTICAL).
  101.    * @return int the result of the evaluation
  102.    */
  103.   public static int eval_fun(
  104.     int enc,
  105.     interactor constr_obj,
  106.     int        cnst_val,
  107.     int        orient) 
  108. {
  109.       /* execute code for the encoded function */
  110.       switch(op_code(enc))
  111.     {
  112.       case OP_NONE:
  113.         /* this is an error for external (heavyweight) constraints */
  114.         throw new sub_arctic_error("Can't have \"NONE\" coded in an " + 
  115.                 "external constraint");
  116.  
  117.       case OP_external:
  118.         /* this is an error here because for lightweight constraints,
  119.          * this gets picked up in eval, and for heavyweight constraints
  120.          * which are already external, this would be infinite recursion. */
  121.         throw new sub_arctic_error("Can't have \"external()\" coded in an " + 
  122.                 "external constraint");
  123.  
  124.       case OP_konst:
  125.         return cnst_val;
  126.  
  127.       default:
  128.         /* something is wrong if we get here */
  129.         throw new sub_arctic_error("Improperly encoded constraint found in "+
  130.                      "op0_encoder.eval_fun()");
  131.     }
  132.     }
  133.  
  134.    //had:
  135.    //* @exception bad_value if the encoding, or constrained object/part are 
  136.    //*                      malformed
  137.  
  138.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  139.  
  140.    /** 
  141.     * Evaluate an encoded constraint applied to the given part of the given
  142.     * object.  We assume here that it it has already been determined that
  143.     * the constraint is a 0-op constraint.<p>
  144.     *
  145.     * @param int        enc         the encoding from the constraint
  146.     * @param interactor constr_obj  the object being constrained
  147.     * @param int        constr_part the part being constrained
  148.     * @param int        orient      the orientation of the constraint
  149.     * @return int the result of the evaluation
  150.     */
  151.    public static int eval(
  152.      int        enc, 
  153.      interactor constr_obj, 
  154.      int        constr_part,
  155.      int        orient) 
  156. {
  157.        int               cnst_val; 
  158.        provider_part_ref ext;
  159.        Object            val;
  160.        int               opcd;
  161.  
  162.        /* extract the constant */
  163.        cnst_val = const_val(enc);
  164.  
  165.        /* execute code for the encoded function */
  166.        opcd = op_code(enc);
  167.  
  168.        /* pull out none and external as special cases */
  169.        if (opcd == OP_NONE)
  170.      {
  171.        /* this should have been picked out earlier but... treat as no-op */
  172.        return constr_obj.get_part(constr_part);
  173.      }
  174.        else if (opcd == OP_external)
  175.      {
  176.        /* get the external value provider associated with the constrained
  177.         * object and part */
  178.        ext = constr_obj.get_external_constraint(constr_part);
  179.  
  180.        /* if there's no external attached we are in trouble */
  181.        if (ext == null)
  182.          throw new sub_arctic_error("External provider is null for " + 
  183.                     "external constraint");
  184.        
  185.        /* get our value from the external provider */
  186.        val = ext.obj.get_value(ext.part_num); 
  187.        
  188.        /* if its not an integer we are in trouble */
  189.        if (!(val instanceof Integer))
  190.          throw new sub_arctic_error("Non-integer produced by external " +
  191.                     "constraint");
  192.        
  193.        /* return the value */
  194.        return ((Integer)val).intValue();
  195.      }
  196.        else
  197.      /* handle the rest normally */
  198.      return eval_fun(enc, constr_obj, cnst_val, orient);
  199.      }
  200.  
  201.     //had:
  202.     //* @exception bad_value if the encoding, or constrained object/part are 
  203.     //*                      malformed
  204.  
  205.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  206.  
  207.    /** Test whether the given encoded constraint (constraining the given 
  208.     *  object) depends on the indicated object and part (expressed relative
  209.     *  to it).  Here we assume that the encoding is already known to contain
  210.     *  a 0-op constraint.<p>
  211.     *
  212.     * @param int        enc             The encoding for the constraint.
  213.     * @param interactor constr_obj      The object the constraint is attached to
  214.     * @param int        test_which_obj  The object we are asking about.  This 
  215.     *                                   can be one of the values OBJCODE_SELF, 
  216.     *                                   OBJCODE_PARENT, OBJCODE_SOME_CHILD, 
  217.     *                                   OBJCODE_PREV_SIBLING, or 
  218.     *                                   OBJCODE_NEXT_SIBLING.  
  219.     * @param int        test_which_part The part we are asking about.
  220.     * @param int        nth_child       for SOME_CHILD, this provides the index
  221.     *                    of the child (and is ignored otherwise).
  222.     * @param int        orient          Orientation of the constraint.  This
  223.     *                                   should be HORIZONTAL or VERTICAL.
  224.     * @return boolean whether the given constraint depends upon the given value
  225.     */
  226.    public static boolean depends_on(
  227.      int        enc, 
  228.      interactor constr_obj,
  229.      int        which_obj, 
  230.      int        which_part, 
  231.      int        nth_child,
  232.      int        orient) 
  233. {
  234.        /* no operands (and currently no implicit dependencies) so we never 
  235.     * depend directly on our neighbors. */
  236.        return false;
  237.      }
  238.  
  239.     //had:
  240.     //* @exception bad_value if one of the parameters has an unrecognized value
  241.  
  242.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  243.  
  244.   /**
  245.    * Extract the set of objects/parts that a constraint depends on.  This
  246.    * produces a Vector with pairs of entries, the first being an interactor,
  247.    * and the second being an Integer which is the part number of that 
  248.    * interactor that is depended upon.<p>
  249.    *
  250.    * Here we already know this is a 0 operand constraint so just do implicit
  251.    * operands.<p>
  252.    * 
  253.    * @param int        enc        encoding value for the constraint in question.
  254.    * @param interactor constr_obj the object the constraint is attached to 
  255.    *                              (hence its referents are relative to).
  256.    * @param int        orient     Orientation of the constraint.  This 
  257.    *                              should be HORIZONTAL or VERTICAL.
  258.    * @return Vector containing pairs of objects, the first being an interactor
  259.    *                which is depended upon, and the second being an Integer
  260.    *                giving the part number of the part depended upon.
  261.    */
  262.   public static Vector mk_depend_list(int enc, interactor constr_obj,int orient)
  263.     {
  264.       /* no operands so we just have implicits */
  265.       return mk_implicit_depend_list(enc, constr_obj, orient);
  266.     }
  267.  
  268.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  269.  
  270.   /**
  271.    * Extract the set of objects/parts that a particular constraint function 
  272.    * implicitly depends on.  This produces a Vector with pairs of entries, the 
  273.    * first being an interactor, and the second being an Integer which is the 
  274.    * part number of that interactor that is depended upon.<p>
  275.    *
  276.    * @param int        enc        encoding value for the constraint in question.
  277.    * @param interactor constr_obj the object the constraint is attached to 
  278.    *                              (hence its referents are relative to).
  279.    * @param int        orient     Orientation of the constraint.  This 
  280.    *                              should be HORIZONTAL or VERTICAL.
  281.    * @return Vector containing pairs of objects, the first being an interactor
  282.    *                which is depended upon, and the second being an Integer
  283.    *                giving the part number of the part depended upon.
  284.    */
  285.   public static Vector mk_implicit_depend_list(
  286.     int enc, interactor constr_obj,int orient)
  287.     {
  288.       /* no implicit operands as of yet, so we return an empty result */
  289.       return new Vector(1);
  290.     }
  291.  
  292.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  293. }
  294.  
  295. /*=========================== COPYRIGHT NOTICE ===========================
  296.  
  297. This file is part of the subArctic user interface toolkit.
  298.  
  299. Copyright (c) 1996 Scott Hudson and Ian Smith
  300. All rights reserved.
  301.  
  302. The subArctic system is freely available for most uses under the terms
  303. and conditions described in 
  304.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  305. and appearing in full in the lib/interactor.java source file.
  306.  
  307. The current release and additional information about this software can be 
  308. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  309.  
  310. ========================================================================*/
  311.